Detailed discussion on the conversion between C picture and byte [] and between byte [] and string

  • 2021-12-09 09:42:46
  • OfStack

Examples are as follows:


// Mainly through Stream As an intermediate bridge 
public static Image ByteArrayToImage(byte[] iamgebytes) {
  MemoryStream ms = new MemoryStream(iamgebytes);
  Image image = Image.FromStream(ms);
  return image;
}

public static byte[] ImageToByteArray(Image image) {
  MemoryStream ms = new MemoryStream();
  image.Save(ms, image.RawFormat);
  return ms.ToArray();
}

public static string ByteArrayToString(byte[] bytes) {
  return Convert.ToBase64String(bytes);
}

public static string StringToByteArray(string image) {
  return Convert.FromBase64String(image);
}

Related articles: